package edu.hawaii.duedates.book;

import java.util.Calendar;

/**
 * The Book Class implements the IBook interface. A book is comprised of a title and a due date.
 * 
 * @author Ronn Reeves and Robin E. Raqueno
 */
public class Book implements IBook {
  /** Unique identifier for this instance. */
  private static final long serialVersionUID = 1L;

  /** The due date for the book. */
  private Calendar dueDate;

  /** The library the book belongs to. */
  private String library;

  /** The book title. */
  private String title;
  
  /**
   * Creates a book object with the given title and due date for a specific library class.
   * 
   * @param title The book title.
   * @param dueDate The due date of the book.
   * @param library The library the book belongs to.
   */
  public Book(String title, Calendar dueDate, String library) {
    this.formatTitle(title);
    this.dueDate = dueDate;
    this.library = library;
  }  

  /**
   * Strips up to 75 characters of the title of this book from extra data in the title string.
   * 
   * @param title The book title (up to 75 characters).
   */
  private void formatTitle(String title) {
    String s1 = title;
    String location = "Location:";

    if (s1.contains(location)) {
      boolean hasDelimiter = s1.contains("/");
      if (hasDelimiter) {
        // The title lies between the first occurrence of "  " and the last occurrence of "/"
        // in the displayed out of the University of Hawaii website.
        if (s1.indexOf("  ") < s1.indexOf('/')) {
          s1 = s1.substring(s1.indexOf("  ") + 2, s1.lastIndexOf('/') - 1);
        }

        // The title lies between the start of the title through the last occurrence of "/"
        // in the displayed out of the University of Hawaii website.
        else {
          s1 = s1.substring(0, s1.lastIndexOf('/') - 1);
        }
      }

      else {
        // The title lies between the first occurrence of "   " and the last occurrence of
        // "Location:" in the displayed out of the University of Hawaii website.
        if ((s1.indexOf("   ") + 3) < s1.indexOf(location)) {
          s1 = s1.substring(s1.indexOf("   ") + 3, s1.lastIndexOf(location));
        }

        // The title lies between the start of the title through the last occurrence of
        // "Location:" in the displayed out of the University of Hawaii website.
        else {
          s1 = s1.substring(0, s1.lastIndexOf(location));
        }
      }
    }

    if (s1.length() > 75) {
      this.title = s1.substring(0, 75);
    }
    else {
      this.title = s1;
    }
  }

  /**
   * Returns the due date for this book.
   * 
   * @return Date The due date for the book.
   */
  public Calendar getDueDate() {
    return this.dueDate;
  }

  /**
   * Returns the library the book is from.
   * 
   * @return Library The library the book is from.
   */
  public String getLibrary() {
    return this.library;
  }

  /**
   * Returns the title of this book.
   * 
   * @return String The book title.
   */
  public String getTitle() {
    return this.title;
  }

  /**
   * Sets the due date of this book.
   * 
   * @param dueDate The due date of the book.
   */
  public void setDueDate(Calendar dueDate) {
    this.dueDate = dueDate;
  }

  /**
   * Sets the library the book is from.
   * 
   * @param library The library the book is from.
   */
  public void setLibrary(String library) {
    this.library = library;
  }

  /**
   * Sets the title of this book.
   * 
   * @param title The book title.
   */
  public void setTitle(String title) {
    this.formatTitle(title);
  }

  /**
   * Returns the title of this book.
   * 
   * @return String The book title.
   */
  public String toString() {
    return this.getTitle();
  }
}